<?php

namespace App\Http\Controllers\Admin;

use App\Http\Model\Admin_user;
use App\Http\Model\Role;
use Validator;
use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Crypt;
use Illuminate\Support\Facades\Input;
use App\Services\OSS;

use DB;

class Admin_userController extends CommonController
{


     public function auth($id)
    {

        $user = Admin_user::find($id);
        $roles = Role::get();

        return view('admin.auth.add',compact('user','roles'));
    }

    

    public function add(Request $request)
    {
        //向 blog_role_user添加授权数据
//        dd($request->all());
        $input = $request->except('_token');
        if($input){
            foreach($input['role_id'] as $k=>$v){
                \DB::insert('insert into role_user (user_id, role_id) values (?, ?)', [$input['id'], $v]);
            }
            return "添加成功！";
        }else{
            return "添加失败！";
        }
    }
    
    

    //图片上传
     public function upload()
    {
//        dd(Input::all());
        $file = Input::file('file_upload');
        if($file->isValid()){
            $entension = $file->getClientOriginalExtension();//上传文件的后缀名
            $newName = date('YmdHis').mt_rand(1000,9999).'.'.$entension;


//            $disk = \Storage::disk('qiniu');
//            七牛的上传方法
//            \Storage::disk('qiniu')->writeStream('uploads/'.$newName, fopen($file->getRealPath(), 'r'));

//            OSS的上传方法
//            $result = OSS::upload('uploads/'.$newName, $file->getRealPath());


            $path = $file->move(public_path().'/uploads',$newName);
            $filepath = 'uploads/'.$newName;
            return  $filepath;
        }
    }

    //分页浏览信息
    public function index(Request $request)
    {       


        if($request->has('keywords')){
            $input = trim($request->input('keywords'));
            //获取所有的用户记录
            $user = Admin_user::where('username','like','%'.$input.'%')->paginate(5);
            return view('admin.admin_user.index',['data'=>$user,'keys'=>$input]);
        }else{
            $user = Admin_user::paginate(5);
            return view('admin.admin_user.index',['data'=>$user]);
        }

       //获取所有的用户记录
       // $user = Admin_user::paginate(5);
        //给模板传变量的三种方法
        //return view('admin.admin_user.index',['data'=>$user]);
    }
    
   
    
    //添加表单
    public function create()
    {
        return view("admin.admin_user.add");
    }
    
    //执行添加
    public function store(Request $request)
    {
        //获取所需信息
        $input = Input::except('_token');
         
        $rule=[
            'username'=>'required|between:3,18',
            'password'=>'required|between:6,18',
            'phone'=>'required|between:11,11',
        ];
        
        $mess=[
            'username.required'=>'用户名必须输入',
            'username.between'=>'用户名在6到18位之间',
            'password.required'=>'密码必须输入',
            'password.between'=>'密码在6-18位之间',
            'phone.required'=>'电话必须输入',
            'phone.between'=>'手机号必须11位',
        ];

        $validator =  Validator::make($input,$rule,$mess);
        if($validator->passes()){
            $input['password']=Crypt::encrypt($input['password']);
            $input['created_at'] = date('Y-m-d h:i:s',time());
            $re = Admin_user::create($input);
            if($re){
                return redirect('admin/admin_user');
            }else{
                return back()->with('errors','用户添加失败');
            }
        }else{
            return back()->withErrors($validator);
        }


    }

    public function show($id)
    {
        //
    }
    
    //执行删除
    public function destroy($id)
    {
        
        $re =  Admin_user::where('id',$id)->delete();

        
      if($re){
          $data = [
              'status'=>0,
              'msg'=>'用户删除成功'
          ];
      }else{
          $data = [
              'status'=>1,
              'msg'=>'用户删除失败'
          ];
      }
      return $data;
    }
    
    //加载修改表单
    public function edit($id)
    {
        $data =  Admin_user::where('id',$id)->first();

       //可以使用compact函数给模板传变量
       return view('admin.admin_user.edit',compact('data'));
    }
    
    //执行修改
    public function update($id,Request $request)
    {
        //表单验证
        $this->validate($request, [
            'username' => 'required|max:16',
            'phone' => 'required|between:11,11',
        ]);
        $data = $request->only("username","phone","pic");
        //$data['updated_at'] = time();
        $id = DB::table("admin_user")->where("id",$id)->update($data);
        
        if($id>0){
            return redirect('admin/admin_user');
        }else{
            return back()->with("err","修改失败!");
        }
        
    }
    



}
